package com.yk.i_wms.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yk.i_wms.common.model.Result;
import com.yk.i_wms.dto.SmsPoDocDTO;
import com.yk.i_wms.mapper.SmsPoDocMapper;
import com.yk.i_wms.model.SmsPoDoc;
import com.yk.i_wms.model.SmsPoDocDetail;
import com.yk.i_wms.service.ISmsPoDocDetailService;
import com.yk.i_wms.service.ISmsPoDocService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 采购单
 *
 * @author hbq
 * @date 2021-03-31 18:24:31
 */
@Slf4j
@Service
public class SmsPoDocServiceImpl extends ServiceImpl<SmsPoDocMapper, SmsPoDoc> implements ISmsPoDocService {
    @Resource
    private SmsPoDocMapper smsPoDocMapper;
    @Autowired
    private ISmsPoDocDetailService smsPoDocDetailService;
    /**
     * 列表
     * @param params
     * @return
     */
    public Page<Map> findList(Map<String, Object> params){
        Integer pageNum = MapUtils.getInteger(params, "pageNum");
        Integer pageSize = MapUtils.getInteger(params, "pageSize");
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = -1;
        }
        Page<Map> pages = new Page<>(pageNum, pageSize);
        return smsPoDocMapper.findList(pages, params);
    }

    @Override
    public Result saveAll(SmsPoDocDTO smsPoDocDTO) {
        SmsPoDoc smsPoDoc=smsPoDocDTO;
        if (smsPoDoc.getState()!=0){
            return Result.failed("非新建状态不允许修改");
        }
        log.info("新建或更新采购单：{}",smsPoDocDTO);
        String poNo =StrUtil.isBlank(smsPoDocDTO.getPoNo())?getNewNo():smsPoDocDTO.getPoNo();
        smsPoDoc.setPoNo(poNo);
        //删除原有docNo的详情
        smsPoDocDetailService.remove(new LambdaQueryWrapper<SmsPoDocDetail>().eq(SmsPoDocDetail::getPoNo,poNo));
        List<SmsPoDocDetail> smsPoDocDetails = smsPoDocDTO.getSmsPoDocDetails();
        if (smsPoDocDetails == null||smsPoDocDetails.isEmpty()) {
            return Result.failed(  "详情列表不能为空");
        }
        smsPoDocDetails.forEach(u->{
            u.setPoNo(poNo);
        });
        saveOrUpdate(smsPoDoc);
        smsPoDocDetailService.saveBatch(smsPoDocDetails);
        return Result.succeed("保存成功");
    }

    @Override
    public Result delete(Map<String, List<Long>> map) {
        List<Long> ids = map.get("ids");
        for (Long id : ids) {
            SmsPoDoc poDoc = getById(id);
            String poNo = poDoc.getPoNo();
            removeById(id);
            smsPoDocDetailService.remove(new LambdaQueryWrapper<SmsPoDocDetail>()
                    .eq(SmsPoDocDetail::getPoNo,poNo)
            );
        }
        return Result.succeed("删除成功");
    }

    /**
     * 获取新的串号
     * @return 当前串号的下一个串号
     */
    private String getNewNo(){
        //生成新的串号
        String prefix="PO"+DateUtil.format(new Date(), "yyyyMMdd");
        String maxNo=prefix+"001";
        SmsPoDoc one = getOne(new QueryWrapper<SmsPoDoc>()
                .last("where po_no like '" + prefix + "%' order by po_no desc limit 1"));
        if (one != null) {
            Long aLong = Convert.toLong(StrUtil.removePrefix(one.getPoNo(), prefix));
            maxNo=prefix+String.format("%3d",aLong+1).replace(" ","0");
        }
        return maxNo;
    }
}
